"""
 -----*--------------*-------
__author__ :  chenzhixiong
初始化APP
-*- coding: utf-8 -*-
 -----*--------------*-------
"""
import os
import time
from common.conf_handle import myconf
from common.path_handler import Config
from common.loger_handler import mylog


class Int_App():
    """初始化APP"""

    def __init__(self, driver):
        """重新父类的初始化"""

        # 实例化
        self.driver = driver

    def InstallApp(self):
        mylog.info("---------------------卸载并重新安装")
        os.system(F"adb -s {myconf.get('APP','devices')} uninstall com.gdu.pro2")
        appapk_path = os.path.join(Config.datas_path, "GduFlight_gdu_release_V2.4.12_C662_20241212-2212.apk")
        os.system(F"adb -s {myconf.get('APP', 'devices')} install {appapk_path}")
        time.sleep(10)

    def SignAPP(self):
        """登录操作"""
        mylog.info("---------------------登录APP")
        self.driver(resourceId="com.gdu.launcher:id/tvStart").click()
        self.driver.xpath("//*[contains(@text,'同意并继续')]").click()
        self.driver(resourceId="com.gdu.pro2:id/et_login_email").send_keys("18186466491")
        self.driver(resourceId="com.gdu.pro2:id/et_login_pwd").send_keys("Ab123456")
        self.driver.press("back")
        time.sleep(1)
        self.driver(resourceId="com.gdu.pro2:id/incSubLayout1").click()
        time.sleep(3)
        if self.driver(resourceId="com.gdu.pro2:id/title").exists():
            self.driver(resourceId="com.gdu.pro2:id/layout_1btn").click()


    def openDebug(self):
        """开启调试模式"""
        mylog.info("---------------------开启调试模式")
        time.sleep(3)
        for i in range(5):
            self.driver(resourceId="com.gdu.pro2:id/ivFlightIcon").click()
        time.sleep(3)
        self.driver(resourceId="com.gdu.pro2:id/custom").click()
        time.sleep(1)
        self.driver.click(956, 914)
        self.driver.click(583, 910)
        self.driver.click(1244, 811)
        time.sleep(1)
        self.driver.click(161, 1118)
        time.sleep(1)
        self.driver.click(679, 914)
        time.sleep(1)
        self.driver.click(165, 1126)
        time.sleep(1)
        self.driver.click(145, 1022)
        time.sleep(1)
        self.driver.click(564, 914)
        time.sleep(1)
        self.driver.click(491, 802)
        self.driver.click(1155, 1010)
        self.driver.click(1244, 802)
        self.driver.click(948, 922)
        self.driver.click(1144, 410)
        self.driver(resourceId="com.gdu.pro2:id/tvUpgradeFirmware").click()

    def open_Manual(self):
        """进入手动飞行"""
        mylog.info("---------------------进入手动飞行")
        self.driver.press("home")
        self.driver(resourceId="com.gdu.launcher:id/tvStart").click()
        self.driver(resourceId="com.gdu.pro2:id/ivManualFlyBtn").click()
        time.sleep(2)
        self.driver(resourceId="com.gdu.pro2:id/iv_closeBtn").click()
        time.sleep(2)
        if self.driver(resourceId="com.gdu.pro2:id/tv_confirm").exists():
            self.driver(resourceId="com.gdu.pro2:id/tv_confirm").click()

    def open_simulation(self):
        """开启模拟飞行"""
        mylog.info("---------------------开启模拟飞行")
        time.sleep(1)
        self.driver(resourceId="com.gdu.pro2:id/iv_zorroRealcontrol_set").click()
        time.sleep(1)
        self.driver.swipe(1213, 999, 1274, 226)
        time.sleep(2)
        self.driver(resourceId="com.gdu.pro2:id/iv_switch_simulate").click()
        time.sleep(2)
        self.driver.press("back")    # 还在手动飞行页面


    def off_Obstacle_avoidance(self):
        # # 关闭返航避障
        mylog.info("---------------------关闭返航避障")
        time.sleep(1)
        self.driver(resourceId="com.gdu.pro2:id/iv_zorroRealcontrol_set").click()
        time.sleep(1)
        self.driver.xpath('//*[@resource-id="com.gdu.pro2:id/rv_left"]/android.view.ViewGroup[6]').click()
        time.sleep(2)
        types = self.driver(resourceId="com.gdu.pro2:id/iv_switch_vision_obstacle").info["selected"]
        time.sleep(1)
        if types:
            self.driver(resourceId="com.gdu.pro2:id/iv_switch_vision_obstacle").click()
            self.driver(resourceId="com.gdu.pro2:id/dialog_btn_sure").click()
        types2 = self.driver(resourceId="com.gdu.pro2:id/iv_goHomeObstacleSwitch").info["selected"]
        time.sleep(1)
        if types2:
            self.driver(resourceId="com.gdu.pro2:id/iv_goHomeObstacleSwitch").click()
        time.sleep(1)
        self.driver.press("back")   # 还在手动飞行页面


    def off_Limit_height_and_distance(self):
        # 关闭限高线距
        mylog.info("---------------------关闭限高线距")
        time.sleep(1)
        self.driver(resourceId="com.gdu.pro2:id/iv_zorroRealcontrol_set").click()
        time.sleep(1)
        state1 = self.driver(resourceId="com.gdu.pro2:id/iv_switch_limit_height").info["selected"]
        if state1:
            self.driver(resourceId="com.gdu.pro2:id/iv_switch_limit_height").click()
            self.driver.xpath("//*[contains(@text,'同意')]").click()

        state2 = self.driver(resourceId="com.gdu.pro2:id/iv_switch_limit_distance").info["selected"]
        if state2:
            self.driver(resourceId="com.gdu.pro2:id/iv_switch_limit_distance").click()
        time.sleep(1)
        self.driver.press("back")   # 还在手动飞行页面






